package com.offcn.webui.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.webui.service.MemberServiceFeign;
import com.offcn.webui.service.ProjectServiceFeign;
import com.offcn.webui.vo.resp.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("project")
public class ProjectController {

    //注入feign远程调用项目服务接口
    @Autowired
    private ProjectServiceFeign projectServiceFeign;

    @Autowired
    private MemberServiceFeign memberServiceFeign;


    @RequestMapping("projectInfo")
    public String projectInfo(Integer id, Model model, HttpSession session){

        AppResponse<ProjectDetailVo> appResponse = projectServiceFeign.detail(id);

        //获取响应的项目详情数据
        ProjectDetailVo projectDetailVo = appResponse.getData();
        //把获取到的项目详情对象封装到model
        model.addAttribute("DetailVo",projectDetailVo);
        //把获取到项目详情对象封装到session
        session.setAttribute("DetailVo",projectDetailVo);

        //跳转到项目详情页
        return "project/project";
    }

    //获取项目回报
    @RequestMapping("/confirm/returns/{projectId}/{returnId}")
    public String returnInfo(@PathVariable("projectId") Integer projectId, @PathVariable("returnId") Integer returnId, Model model, HttpSession session){

        //从session获取项目详情信息
     ProjectDetailVo projectDetailVo= (ProjectDetailVo) session.getAttribute("DetailVo");
        AppResponse<ReturnPayConfirmVo> appResponse = projectServiceFeign.returnInfo(returnId);
        ReturnPayConfirmVo returnPayConfirmVo = appResponse.getData();

        //设置项目id给项目回报对象
        returnPayConfirmVo.setProjectId(projectId);

        //设置项目名称
        returnPayConfirmVo.setProjectName(projectDetailVo.getName());

        //根据项目id，获取用户详情信息

        AppResponse<UserRespVo> userRespVoAppResponse = memberServiceFeign.findUser(projectDetailVo.getMemberid());
        UserRespVo userRespVo = userRespVoAppResponse.getData();

        //设置项目发起方的名称
        returnPayConfirmVo.setMemberName(userRespVo.getRealname());

        //封装回报数据到model
        model.addAttribute("returnConfirm",returnPayConfirmVo);
        session.setAttribute("returnConfirm",returnPayConfirmVo);
        return "project/pay-step-1";
    }

    //跳转到项目支付确认页面
    @RequestMapping("/confirm/order/{num}")
    public String confirmOrder(@PathVariable("num") Integer num,Model model,HttpSession session){

        //1、从sesion读取当前登录用户数据对象
     UserRespVo userRespVo= (UserRespVo) session.getAttribute("sessionMember");

     //2、判断用户是否登录
        if(userRespVo==null){
            //用户登录成功后需要再跳转回当前页面
            session.setAttribute("preUrl","project/confirm/order/"+num);
            //跳转到登录页面
            return "redirect:/login.html";
        }

        //3、从用户对象获取AccessTOKEN
        String accessToken = userRespVo.getAccessToken();

        //4、使用accessToken调用远程用户服务，获取对应地址集合
        AppResponse<List<UserAddressVo>> appResponse = memberServiceFeign.address(accessToken);

        List<UserAddressVo> userAddressVoList = appResponse.getData();

        model.addAttribute("addresses",userAddressVoList);

        //5、从session获取上一步项目确认信息
        ReturnPayConfirmVo  returnPayConfirmVo= (ReturnPayConfirmVo) session.getAttribute("returnConfirm");
        //6、设置支持数量
        returnPayConfirmVo.setNum(num);
        //7、设置项目合计金额
        returnPayConfirmVo.setTotalPrice(new BigDecimal(num*returnPayConfirmVo.getSupportmoney()+returnPayConfirmVo.getFreight()));

        session.setAttribute("returnConfirmSession",returnPayConfirmVo);
        //跳转到回报确认页面
        return "project/pay-step-2";
    }
}
